//
//  KLCollectionViewController.m
//  KunLun
//
//  Created by Mac on 2017/11/28.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLCollectionViewController.h"
#import "KLCollectionShopTableViewCell.h"
#import "KLCollectionGoodTableViewCell.h"
@interface KLCollectionViewController ()
@property(nonatomic, strong) STSegementButton                     *segementButton;
@property(nonatomic, strong) STNoresultView                     *noresultView;
@end

@implementation KLCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"收藏";
    [self configSubView];
    self.automaticallyAdjustsScrollViewInsets = NO;
#ifdef ios11
    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
#endif
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
 
    __weak typeof(self) weakSelf =  self;
    self.segementButton = [[STSegementButton alloc] initWithFrame:CGRectMake(0, 64, UIScreenWidth, 44) andTitle:@[@"商品",@"店铺"] handle:^(STSegementButton *sender) {
        //置空数据源，并且刷新，之后mj开始
        [weakSelf.tableView reloadData];
        [self.tableView st_showAnimationWithType:STAnimationTypekCATransitionFade duration:0.25];
    }];
    self.segementButton.backgroundColor = [UIColor whiteColor];
    self.segementButton.lineSelectedColor = KL_BlueBackGroundColor;
    self.segementButton.butTitleSelectedColor = KL_BlueBackGroundColor;
    self.segementButton.butTitleColor = KL_secendTextColor;
    self.segementButton.lineColor = [UIColor whiteColor];
    self.segementButton.cureentIndex = 0;
    self.segementButton.autoMoveWithClic = YES;
    [self.view addSubview:self.segementButton];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.top = self.tableView.top + self.segementButton.height + 64;
    self.tableView.height = self.tableView.height - self.segementButton.height - 64;
    self.noresultView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 180, UIScreenWidth, 300)
                                                        title:@"抱歉,暂无该收藏"
                                                  buttonTitle:@""
                                                 buttonHandle:^(NSString *titleString) {
                                                     
                                                 }];
    self.noresultView.hidden = YES;
    [self.tableView addSubview:self.noresultView];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
    }];
    
    //注册tableView
    [self.tableView registerClass:[KLCollectionGoodTableViewCell class] forCellReuseIdentifier:@"good"];
    [self.tableView registerClass:[KLCollectionShopTableViewCell class] forCellReuseIdentifier:@"shop"];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.segementButton.cureentIndex == 0) {
        return [KLCollectionGoodTableViewCell cellHeight];
        
    }
    return [KLCollectionShopTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.segementButton.cureentIndex == 0) {
        return 54;
        
    }
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.segementButton.cureentIndex == 0) {
        __weak typeof(self) weakSelf =  self;
        UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth , 64)];
        sectionView.backgroundColor = self.tableView.backgroundColor;
        STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 10, UIScreenWidth, 44)
                                                         title:@"店铺名字"
                                                    titleColor:KL_firstTextColor
                                                     titleFont:15
                                                  cornerRadius:0
                                               backgroundColor:[UIColor whiteColor]
                                               backgroundImage:nil
                                                         image:[UIImage imageNamed:@"购物未选中"]];
        [buyButton setImage:[UIImage imageNamed:@"购物选中"] forState:UIControlStateSelected];
        buyButton.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        buyButton.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
        [buyButton setClicAction:^(UIButton *sender) {
            sender.selected = !sender.selected;
        }];
        [sectionView addSubview:buyButton];
        return sectionView;
        
    }
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.segementButton.cureentIndex == 0) {
        KLCollectionGoodTableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"good"];
        return cell;
    }else{
        KLCollectionShopTableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"shop"];
        return cell;
    }

}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

@end
